# barco/sandboxconfig.py

import os
import sys

from . import error 
from . import sandboxmodule

class SandboxConfig(object):

    ##
    ## The Info Class
    ## Stores information on how the Sandbox is configured
    ##

    class Info(object):

        def __init__(self):
            self.src_branch = 'main/dev'
            self.bin_branch = 'main/dev'
            self.src_directory = 'src'
            self.bin_directory = 'install'
            self.modules = dict()
            return None

        def get_src_directory(self):
            return self.src_directory
        
        def get_src_branch(self):
            return self.src_branch
        
        def get_bin_directory(self):
            return self.bin_directory
        
        def get_bin_branch(self): 
            return self.bin_branch     

        def set_src_directory(self, directory): 
            self.src_directory = directory
            return None

        def set_src_branch(self, branch): 
            self.src_branch = branch
            return None
        
        def set_bin_directory(self, directory): 
            self.bin_directory = directory
            return None
        
        def set_bin_branch(self, branch): 
            self.bin_branch = branch
            return None

        def add_module(self, module, properties):
            self.modules[module] = properties
            return None

        def get_modules(self):
            return self.modules.keys()

    ##
    ## The Sandbox class that will be made available from the
    ## "Barco" scope inside a configuration file
    ##
    class Sandbox(object):
        
        def __init__(self, info, *args, **argv):
            self.info = info
            if 'srctag' in argv: self.info.set_src_branch(argv['srctag'])
            if 'bintag' in argv: self.info.set_bin_branch(argv['bintag'])
            if 'srcdir' in argv: self.info.set_src_directory(argv['srcdir'])
            if 'bindir' in argv: self.info.set_bin_directory(argv['bindir'])
            return None

        def Module(self, *args, **argv):
            for m in args: 
                self.info.add_module(str(m), argv)
            return self

    ##
    ## Barco scope that will be exported into the configuration file
    ## Simply modeled as a class 
    ##
    class Barco(object):
        
        def __init__(self, parent):
            self.parent = parent
            return None
 
        def Sandbox(self, *args, **argv):
            return SandboxConfig.Sandbox(self.parent, *args, **argv)

    ## initialize 
    def __init__(self):
        self.config = os.path.join(os.getcwd(), 'bsb.conf')
        return None

    def set(self, config):
        self.config = config
        return None

    def process(self):
      
        # Do we read from stdin (-) or a file
        if self.config == '-':
            buf = sys.stdin.read()        
        else:
            try: 
                f = open(self.config, 'r')                
                buf = f.read()
                f.close()
            except:
                raise error.Error('Can\'t read configuration file \'%s\'' % self.config)
            
        # Create the info structure
        info = SandboxConfig.Info()

        # Python logic to include and execute the configration file.
        # No globals, and just the Barco scope is available in the locals
        exec compile(buf, '<config>', 'exec') in {}, { 'Barco': SandboxConfig.Barco(info) }        

        return info


